﻿using System;
using System.Collections;
using System.Configuration;
using System.Globalization;

namespace DMS.Integration
{
    public class InvoiceDocMetadata : IMetadata
    {

        private static string _invoiceDocumentLibrary = "InvoiceDocuments";

        private static string _invoiceWeb = "Invoices";

        //document number
        public string Id { get; set; }
        public string BranchName { get; set; }
        //public int BranchId { get; set; }

        public string AuctionID { get; set; }
        public string ConsignmentGoodNumber { get; set; }
        public string ConsignmentVendorNumber { get; set; }
        public string GoodGUID { get; set; }
        public string GoodID { get; set; }
        public DateTime InvoiceDate { get; set; }
        public string InvoiceGUID { get; set; }
        public string InvoiceNumberSuffix { get; set; }
        public string PlateNumber { get; set; }
        public string InvoiceType { get; set; }
        public string VIN { get; set; }
        public string DocumentType { get; set; }
        public string CustomerName { get; set; }
        public string CustomerNumber { get; set; }
        public string DocumentName { get; set; }
        public decimal InvoiceTotal { get; set; }

        private string _siteCollectionUrl = ConfigurationManager.AppSettings["SharePointDMSSiteUrl"];
        public string SiteCollectionUrl
        {
            get { return _siteCollectionUrl; }
            set { _siteCollectionUrl = value; }
        }

        public int Year { get; set; }

        public string DocumentLink
        {
            get
            {
                if (!SiteCollectionUrl.EndsWith("/"))
                    SiteCollectionUrl += "/";
                return string.Format("{0}{1}/{2}/{3}/{4}/{5}.pdf, {5}.pdf", SiteCollectionUrl, _invoiceWeb, Year, _invoiceDocumentLibrary, GetFolderName(Id), Id);
            }
        }
        private string GetFolderName(string filename)
        {
            int suffixdot = filename.IndexOf(".", System.StringComparison.Ordinal);
            int separater = filename.LastIndexOf("-", StringComparison.CurrentCultureIgnoreCase);
            if (suffixdot < separater || suffixdot == -1)
            {
                if (separater != -1)
                    return filename.Substring(separater + 1);
            }
            string folderName = filename.Substring(separater + 1, suffixdot - separater - 1);
            return folderName;
        }

        public Hashtable ToStringDictionary()
        {
            Hashtable dict = new Hashtable();
            dict.Add("Title", Id);
            dict.Add("DocumentName", Id);
            dict.Add("DocumentLink", DocumentLink.ToString(CultureInfo.InvariantCulture));
            if (!string.IsNullOrEmpty(AuctionID)) dict.Add("AuctionID", AuctionID);
            if (!string.IsNullOrEmpty(ConsignmentGoodNumber)) dict.Add("ConsignmentGoodNumber", ConsignmentGoodNumber);
            if (!string.IsNullOrEmpty(ConsignmentVendorNumber)) dict.Add("ConsignmentVendorNumber", ConsignmentVendorNumber);
            if (!string.IsNullOrEmpty(GoodGUID)) dict.Add("GoodGUID", GoodGUID);
            if (!string.IsNullOrEmpty(GoodID)) dict.Add("GoodID", GoodID);
            if (!string.IsNullOrEmpty(InvoiceGUID)) dict.Add("InvoiceGUID", InvoiceGUID);
            if (!string.IsNullOrEmpty(InvoiceNumberSuffix)) dict.Add("InvoiceNumberSuffix", InvoiceNumberSuffix);
            if (!string.IsNullOrEmpty(InvoiceType)) dict.Add("InvoiceType", InvoiceType);
            if (!string.IsNullOrEmpty(VIN)) dict.Add("VIN", VIN);
            if (!string.IsNullOrEmpty(DocumentType)) dict.Add("DocumentType", DocumentType);
            if (!string.IsNullOrEmpty(BranchName)) dict.Add("BranchName", BranchName);
            if (!string.IsNullOrEmpty(PlateNumber)) dict.Add("PlateNumber", PlateNumber);
            if (!string.IsNullOrEmpty(CustomerName)) dict.Add("CustomerName", CustomerName);
            if (!string.IsNullOrEmpty(CustomerNumber)) dict.Add("CustomerNumber", CustomerNumber);
            if (!string.IsNullOrEmpty(DocumentName)) dict.Add("DocumentName", DocumentName);
            if (InvoiceTotal != 0.0M) dict.Add("InvoiceTotal", InvoiceTotal.ToString(CultureInfo.InvariantCulture));

            if (!InvoiceDate.Equals(DateTime.MinValue)) dict.Add("InvoiceDate", InvoiceDate.ToString("yyyy-MM-ddTHH:mm:ssZ"));

            return dict;
        }
    }
}